package com.example.any_testdemo.sqliteDemo

import android.content.ContentValues
import android.database.Cursor
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.any_testdemo.R
import com.example.any_testdemo.databinding.ActivityDbTestBinding

class DbTestActivity : AppCompatActivity() {
    private var binding: ActivityDbTestBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDbTestBinding.inflate(layoutInflater);
        setContentView(binding!!.root)
        binding!!.btnDbCreate.setOnClickListener { onClick(it) }
        binding!!.btnDbUpgrade.setOnClickListener { onClick(it) }
        binding!!.btnDbInsert.setOnClickListener { onClick(it) }
        binding!!.btnDbQuery.setOnClickListener { onClick(it) }
    }

    private fun onClick(v: View?) {
        if (v != null) {
            when (v.id) {
                R.id.btn_db_create -> createDb()
                R.id.btn_db_upgrade -> upgradeDb()
                R.id.btn_db_insert -> insertDb()
                R.id.btn_db_query -> queryDb()
                else -> "unUseless"

            }
        }
    }

    private fun queryDb() {
        val dbHelper = TestSqliteOpenHelper(this, "personTest")
        val sqliteDataBase = dbHelper.readableDatabase
        var cursor = sqliteDataBase.query("person", arrayOf("id", "name", "sexType"),"id=?", arrayOf("1"),null,null,null)
        if (cursor.moveToFirst()){
            val name = cursor.getString(cursor.getColumnIndex("name"))
            println("name=$name")
        }
        sqliteDataBase.close()
    }

    private fun createDb() {
        val dbHelper = TestSqliteOpenHelper(this, "personTest")
        var sqliteDataBase = dbHelper.writableDatabase
    }

    private fun upgradeDb() {
        val dbHelper = TestSqliteOpenHelper(this, "personTest")
        var sqliteDataBase = dbHelper.readableDatabase
        var cursor = sqliteDataBase!!.query(
            "person",
            arrayOf("id", "name", "sexType"),
            "id=?",
            arrayOf("1"),
            null,
            null,
            null
        )
        var id = 0
        var name = ""
        while (cursor.moveToNext()) {
            id = cursor.getInt(cursor.getColumnIndex("id"))
            name = cursor.getString(cursor.getColumnIndex("name"))
            if (id == 1) {
                println("id= $id name = $name")
                val contentValues = ContentValues()
                contentValues.put("name","王伟大")
                sqliteDataBase.update("person",contentValues,"id=?", arrayOf(id.toString()))
            }

        }
        sqliteDataBase.close()
    }

    private fun insertDb() {
        var dbHelper = TestSqliteOpenHelper(this, "personTest")
        var sqliteDataBase = dbHelper.writableDatabase
        var values = ContentValues()
        values.put("id", 1)
        values.put("name", "王伟")
        values.put("sexType", 1)
        sqliteDataBase.insert("person", null, values)
        sqliteDataBase.close()
    }

}